package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserFollowsService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Wangyk
 * @date 2019/1/14 11:09
 */
@RestController
@RequestMapping("userFollows")
public class UserFollowsController {

    @Autowired
    private IUserFollowsService userFollowsService;

    /**
     * 查询用户的关注/粉丝 信息
     *
     * @param masterUserId  要查询的用户id
     * @param currentUserId 当前用户id
     * @return 关注/粉丝信息 map
     */
    @GetMapping("{masterUserId}/followInfo")
    public Object getFollowInfo(@PathVariable Long masterUserId, Long currentUserId) {
        //查询用户的关注/粉丝数
        HashMap<String, Object> followInfo = userFollowsService.getFollowInfoByUserId(masterUserId, currentUserId);
        return followInfo;
    }

    /**
     * 添加一个关注关系
     *
     * @param slaveUserId  关注者
     * @param masterUserId 被关注者
     * @return 是否关注成功
     */
    @RequestMapping(method = {RequestMethod.PUT, RequestMethod.POST}, value = "{slaveUserId}")
    public Object addAFollowRelation(@PathVariable Long slaveUserId, Long masterUserId) {
        JSONResult jsonResult = new JSONResult();
        try {
            //添加/取消一个用户关注
            userFollowsService.addOrUpdateAFollow(masterUserId, slaveUserId);
            //重新查询
            HashMap<String, Object> followInfo = userFollowsService.getFollowInfoByUserId(masterUserId, slaveUserId);
            jsonResult.setData(JSONUtil.toJSONString(followInfo));
        } catch (Exception e) {
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    /**
     * 查询一个用户的关注/粉丝信息
     *
     * @param userId 用户id
     * @return 关注, 粉丝信息
     */
    @GetMapping("{userId}")
    public Object getAUserzFollowFans(@PathVariable Long userId) {
        JSONResult jsonResult = new JSONResult();
        try {
            //查询关注/粉丝数据
            Map<String, List<User>> userFollowersFans = userFollowsService.getAUserzFollowersFans(userId);
            jsonResult.setData(userFollowersFans);
        } catch (Exception e) {
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }
}
